package com.yuncheng.oaApi.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.entity.OaXxpx;
import com.yuncheng.oaApi.service.IOaXxpxService;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: 学习培训
 * @Author: lhb
 * @Date: 2023-07-17
 * @Version: V1.0
 */
@Api(tags = "学习培训接口")
@RestController
@RequestMapping("/api/spzx-oa/oaXxpx")
public class OaXxpxController extends BaseController<OaXxpx, IOaXxpxService> {

    private static final Logger log = LoggerFactory.getLogger(OaXxpxController.class);

    @Resource
    private IOaXxpxService oaXxpxService;

    @Resource
    private MyExcelUtils oaMyExcelUtils;

    /**
     * 分页列表查询
     *
     * @param oaXxpx
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "学习培训-分页列表查询")
    @ApiOperation(value = "学习培训-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaXxpx oaXxpx, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaXxpx> queryWrapper = QueryBuilder.initQueryWrapper(oaXxpx, req.getParameterMap());
        Page<OaXxpx> page = new Page<>(pageNo, pageSize);
        IPage<OaXxpx> pageList = oaXxpxService.page(page, queryWrapper);
        for(OaXxpx vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }



    /**
     * 添加
     *
     * @param oaXxpx
     * @return
     */
    @AutoLog(value = "学习培训-添加")
    @ApiOperation(value = "学习培训-添加")
    //@RequiresPermissions("oaxxpx:OaXxpx:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaXxpx oaXxpx) {
        HttpResult<String> result = new HttpResult(true);
        oaXxpxService.save(oaXxpx);
        result.setResult(oaXxpx.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    /**
     * 编辑
     *
     * @param oaXxpx
     * @return
     */
    @AutoLog(value = "学习培训-编辑")
    @ApiOperation(value = "学习培训-编辑")
    //@RequiresPermissions("oaxxpx:OaXxpx:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaXxpx oaXxpx) {
        oaXxpxService.updateById(oaXxpx);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "学习培训-通过id删除")
    @ApiOperation(value = "学习培训-通过id删除")
    //@RequiresPermissions("oaxxpx:OaXxpx:delete")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        oaXxpxService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "学习培训-批量删除")
    @ApiOperation(value = "学习培训-批量删除")
    //@RequiresPermissions("oaxxpx:OaXxpx:delete")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        oaXxpxService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "学习培训-通过id查询")
    @ApiOperation(value = "学习培训-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        OaXxpx oaXxpx = oaXxpxService.getById(id);
        return HttpResult.ok(oaXxpx);
    }

    @AutoLog(value = "导出学习培训记录数据-表格excel")
    @ApiOperation(value = "导出学习培训记录数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportrypxData")
    public void exportrypxData(@RequestParam(value = "ids",defaultValue = "")String ids,
                               OaXxpx ndkpBasic,
                               HttpServletResponse response){
        try {
            String name = "exportXxpxData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaXxpx> ndkpBasicList = oaXxpxService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaXxpx e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaXxpx> queryWrapper = new LambdaQueryWrapper<>();
                //培训单位
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFpxdw()),OaXxpx::getFpxdw, ndkpBasic.getFpxdw());
                //培训内容
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFpxnr()),OaXxpx::getFpxnr,ndkpBasic.getFpxnr());
                //参训人员
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFcxry()),OaXxpx::getFcxry,ndkpBasic.getFcxry());
                //培训开始时间
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFpxkssj()),OaXxpx::getFpxkssj,ndkpBasic.getFpxkssj());
                //培训结束时间
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFpxjssj()),OaXxpx::getFpxjssj,ndkpBasic.getFpxjssj());

                if (ndkpBasic.getFpxkssj() != null){
                    queryWrapper.le(OaXxpx::getFpxkssj,ndkpBasic.getFpxkssj());
                }
                if (ndkpBasic.getFpxjssj() != null){
                    queryWrapper.ge(OaXxpx::getFpxjssj,ndkpBasic.getFpxjssj());
                }
                List<OaXxpx> ndkpBasicList = oaXxpxService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaXxpx e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_RYPX_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出学习培训信息数据出错-{}",e.getMessage());
        }
    }



    /**
     * 数据转换
     * @param oaXxpx
     */
    private void formatValue(OaXxpx oaXxpx){
    }
}
